package com.example.result.explore

import androidx.compose.animation.core.tween
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults.cardColors
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun LazyVerticalGridShow() {
    var cells: GridCells by remember {
        mutableStateOf(GridCells.Adaptive(minSize = 128.dp))
    }
    val coroutine = rememberCoroutineScope()
    val state = rememberLazyGridState()
    LazyVerticalGrid(
        columns = cells,
        verticalArrangement = Arrangement.spacedBy(8.dp),
        horizontalArrangement = Arrangement.spacedBy(16.dp),
        contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp),
        state = state
    ) {
        items(120) { i ->
            Card(modifier = Modifier.animateItemPlacement(
                tween(durationMillis = 250)
            ),
                colors = cardColors(containerColor = MaterialTheme.colorScheme.tertiaryContainer),
                onClick = {
                    if (i % 3 == 0) {
                        cells = GridCells.Adaptive(minSize = 128.dp)
                    } else if (i % 3 == 1) {
                        cells = GridCells.Fixed(3)
                    } else if (i % 3 == 2) {
                        cells = GridCells.FixedSize(128.dp)
                    }
                    if (i > 10) {
                        coroutine.launch {
                            if (i % 2 == 0) {
                                state.scrollToItem(0)
                            } else {
                                state.animateScrollToItem(0)
                            }
                        }
                    }
                }) {
                if (i % 3 == 0) {
                    Text(text = "cells = GridCells.Adaptive(minSize = 128.dp)")
                } else if (i % 3 == 1) {
                    Text(text = "cells = GridCells.Fixed(3)")
                } else if (i % 3 == 2) {
                    Text(text = "cells = GridCells.FixedSize(128.dp)")
                }
                if (i > 10) {
                    if (i % 2 == 0) {
                        Text(text = "state.scrollToItem(0)")
                    } else {
                        Text(text = "state.animateScrollToItem(0)")
                    }
                }

                Text(text = "$i")
            }
        }
    }
}